﻿using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FacialClub.Helpers;
using FacialClub.Models.Entities;
using FacialClub.Models.Interfaces;
using FacialClub.Models.Repositories;
using FacialClub.Models.ViewModels;

namespace FacialClub.Areas.Admin.Controllers
{
    /// <summary>
    /// Treatment Controller
    /// </summary>
    [Authorize]
    public class TreatmentController : Controller
    {
        #region Fields

        private const int DEFAULTPAGESIZE = 10;
        private readonly ITreatmentRepository _treatmentRepository;

        #endregion

        #region Constructors

        /// <summary>
        /// Dependency Injection enabled constructors
        /// </summary>
        public TreatmentController() : this(new TreatmentRepository())
        {
        }

        /// <summary>
        /// Treatment Controller Constructor
        /// </summary>
        /// <param name="treatmentRepository"></param>
        public TreatmentController(ITreatmentRepository treatmentRepository)
        {
            _treatmentRepository = treatmentRepository;
        }

        #endregion

        #region Actions

        /// <summary>
        /// Display the listing of Treatment
        /// </summary>
        /// <returns></returns>
        public ActionResult Index(string strQuery, int? page)
        {
            IQueryable<Treatment> treatment = !string.IsNullOrWhiteSpace(strQuery)
                                                    ? _treatmentRepository.FindTreatmentByName(strQuery).OrderBy(
                                                        d => d.CreatedDate)
                                                    : _treatmentRepository.FindAllTreatment().OrderBy(
                                                        d => d.CreatedDate);
            var paginatedTreatment =
                treatment.ToPagedList(page.HasValue ? page.Value - 1 : 0, DEFAULTPAGESIZE);
            return View(paginatedTreatment);
        }

        /// <summary>
        /// Details of Treatment
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Details(int? id)
        {
            if (id != null)
            {
                var treatment = _treatmentRepository.GetTreatmentById(id.Value);

                if (treatment == null)
                {
                    return new FileNotFoundResult {Message = "No Treatment found for that id"};
                }

                return View(new TreatmentViewModel(treatment));
            }

            return new FileNotFoundResult {Message = "No Treatment found due to invalid Treatment id"};
        }

        /// <summary>
        /// Create Treatment
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public ActionResult Create()
        {
            var treatment = new Treatment();
            return View(new TreatmentViewModel(treatment));
        }

        /// <summary>
        /// Create Treatment
        /// </summary>
        /// <param name="treatment"></param>
        /// <returns></returns>
        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Create(Treatment treatment)
        {
            if (ModelState.IsValid)
            {
                treatment.UserCreated = HttpContext.User.Identity.Name;
                treatment.CreatedDate = DateTime.Now.ToSqlDateTime();
                treatment.IsActive = Convert.ToBoolean(Request["IsActive"]);
                _treatmentRepository.Add(treatment);
                _treatmentRepository.Save();
                return RedirectToAction("Details", new {id = treatment.TreatmentId});
            }

            return View(new TreatmentViewModel(treatment));
        }

        /// <summary>
        /// Edit Treatment
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Authorize]
        public ActionResult Edit(int id)
        {
            var treatment = _treatmentRepository.GetTreatmentById(id);
            return View(new TreatmentViewModel(treatment));
        }

        /// <summary>
        /// Edit Treatment
        /// </summary>
        /// <param name="id"></param>
        /// <param name="collection"></param>
        /// <returns></returns>
        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var treatment = _treatmentRepository.GetTreatmentById(id);
            if (ModelState.IsValid)
            {
                try
                {
                    treatment.UserModified = HttpContext.User.Identity.Name;
                    treatment.ModifiedDate = DateTime.Now;
                    UpdateModel(treatment, "Treatment");
                    _treatmentRepository.Save();
                    return RedirectToAction("Details", new {id = treatment.TreatmentId});
                }
                catch
                {
                    return View(new TreatmentViewModel(treatment));
                }
            }
            return View(new TreatmentViewModel(treatment));
        }

        /// <summary>
        /// Delete Treatment
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Authorize]
        public ActionResult Delete(int id)
        {
            var treatment = _treatmentRepository.GetTreatmentById(id);
            return treatment == null ? View("NotFound") : View(new TreatmentViewModel(treatment));
        }

        /// <summary>
        /// Delete Treatment
        /// </summary>
        /// <param name="id"></param>
        /// <param name="confirmButton"></param>
        /// <returns></returns>
        [HttpPost, Authorize]
        public ActionResult Delete(int id, string confirmButton)
        {
            var treatment = _treatmentRepository.GetTreatmentById(id);

            if (treatment != null)
            {
                _treatmentRepository.Delete(treatment);
                _treatmentRepository.Save();
                return View("Deleted");
            }
            return View("NotFound");
        }

        /// <summary>
        /// Handle Unknown Action
        /// </summary>
        /// <param name="actionName"></param>
        /// <exception cref="HttpException"></exception>
        protected override void HandleUnknownAction(string actionName)
        {
            throw new HttpException(404, "Action not found");
        }

        #endregion
    }
}